package com.gump.essay.juc;

import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @auther zzc
 * @date 2021 04 18
 */
public class BlockQueue {

    public static void main(String[] args) {
        Executor executor = new ThreadPoolExecutor(2, 3, 1L,
                TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>(5),
                new ThreadPoolExecutor.DiscardPolicy());

        String lockA ="loakA";
        String lockB ="loakB";
        executor.execute(()->new deadDemo().method(lockA, lockB));
        executor.execute(()->new deadDemo().method(lockB, lockA));

    }

    //死锁

    static class deadDemo{
       public void method(String a,String b){
            synchronized(a){
                System.out.println(Thread.currentThread().getName()+"获得"+a);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized(b){
                    System.out.println("获得两把锁");
                }
            }
        }
    }

}
